<template>
  <div style="width: 100%">
    <el-col>
      <el-form ref="labelForm" :model="labelForm" label-width="100px" size="mini">
        <el-form-item :label="$t('chart.gauge_option')" class="form-item">
          <el-radio-group v-model="labelForm.optionType" @change="changeLabelAttr()">
            <el-radio label="0">{{ $t('chart.line_symbol_none') }}</el-radio>
            <el-radio label="1">{{ $t('chart.gauge_option') }}1</el-radio>
            <el-radio label="2">{{ $t('chart.gauge_option') }}2</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="labelForm.optionType == '1' || labelForm.optionType == '2'"
          :label="$t('chart.gauge_ratio')"
          class="form-item"
        >
          <el-slider v-model="labelForm.ratio" :min="0" :max="1.5" :step="0.01" @change="changeLabelAttr()"></el-slider>
        </el-form-item>
        <el-form-item
          v-if="!labelForm.optionType || labelForm.optionType === '0'"
          :label="$t('chart.gauge_axis_label')"
          class="form-item"
        >
          <el-select
            v-model="labelForm.axisLabel.fontSize"
            :placeholder="$t('chart.text_fontsize')"
            size="mini"
            @change="changeLabelAttr()"
          >
            <el-option v-for="option in fontSize" :key="option.value" :label="option.name" :value="option.value" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="labelForm.optionType !== '1'" :label="$t('chart.gauge_axis_color')" class="form-item">
          <el-color-picker
            v-model="labelForm.axisLabel.color"
            class="color-picker-style"
            :predefine="predefineColors"
            @change="changeLabelAttr()"
          />
        </el-form-item>
        <el-form-item :label="$t('chart.gauge_title_show')" class="form-item">
          <el-checkbox v-model="labelForm.title.show" @change="changeLabelAttr('show')">{{
            $t('chart.show')
          }}</el-checkbox>
        </el-form-item>
        <el-form-item :label="$t('chart.gauge_title_label')" class="form-item">
          <el-select
            v-model="labelForm.title.textStyle.fontSize"
            :placeholder="$t('chart.text_fontsize')"
            size="mini"
            @change="changeLabelAttr()"
          >
            <el-option v-for="option in fontSize" :key="option.value" :label="option.name" :value="option.value" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('chart.gauge_title_color')" class="form-item">
          <el-color-picker
            v-model="labelForm.title.textStyle.color"
            class="color-picker-style"
            :predefine="predefineColors"
            @change="changeLabelAttr()"
          />
        </el-form-item>
      </el-form>
    </el-col>
  </div>
</template>

<script>
import { COLOR_PANEL, DEFAULT_GAUGE } from '../../chart/chart'

export default {
  name: 'GaugeSelector',
  props: {
    param: {
      type: Object,
      required: true,
    },
    chart: {
      type: Object,
      required: true,
    },
    propertyInner: {
      type: Array,
      required: false,
      default: function () {
        return []
      },
    },
  },
  data() {
    return {
      labelForm: JSON.parse(JSON.stringify(DEFAULT_GAUGE)),
      fontSize: [],
      isSetting: false,
      predefineColors: COLOR_PANEL,
    }
  },
  watch: {
    chart: {
      handler: function () {
        this.initOptions()
        this.initData()
      },
    },
  },
  mounted() {
    this.init()
    this.initData()
  },
  methods: {
    initData() {
      const chart = JSON.parse(JSON.stringify(this.chart))
      if (chart.customAttr) {
        let customAttr = null
        if (Object.prototype.toString.call(chart.customAttr) === '[object Object]') {
          customAttr = JSON.parse(JSON.stringify(chart.customAttr))
        } else {
          customAttr = JSON.parse(chart.customAttr)
        }
        if (customAttr.gauge) {
          this.labelForm = customAttr.gauge
        }
      }
    },
    init() {
      const arr = []
      for (let i = 10; i <= 40; i = i + 2) {
        arr.push({
          name: i + '',
          value: i + '',
        })
      }
      this.fontSize = arr
    },
    changeLabelAttr(modifyName) {
      // this.labelForm['modifyName'] = modifyName
      this.$emit('onGaugeChange', this.labelForm)
    },
  },
}
</script>

<style scoped>
.shape-item {
  padding: 6px;
  border: none;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.form-item-slider ::v-deep .el-form-item__label {
  font-size: 12px;
  line-height: 38px;
}

.form-item ::v-deep .el-form-item__label {
  font-size: 12px;
}

.el-select-dropdown__item {
  padding: 0 20px;
}

span {
  font-size: 12px;
}

.el-form-item {
  margin-bottom: 6px;
}

.switch-style {
  position: absolute;
  right: 10px;
  margin-top: -4px;
}

.color-picker-style {
  cursor: pointer;
  z-index: 1003;
}

::v-deep .el-radio__label {
  font-size: 12px;
}

::v-deep .el-radio {
  padding-bottom: 5px;
}

::v-deep .el-radio-group {
  padding-top: 5px;
}
</style>
